package com.intelligentmeterreadingsys.settingsys.activity;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.settingsys.db.SettingDao;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;

import dbhelper.XmlNameHelper;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * GPS设置
 * @author king
 *
 */
public class SettingGpsActivity extends Activity{
     
	public Spinner spinner1;//手动优先
	
	public Spinner spinner2;//自动优先
	
	public Button button1;//确定
	
	public EditText edit1;//超时
	
	public EditText edit2;//自动周期
	
	public String manual;
	
	public String automatic;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.setting_gps);
		init();
		Listent();
	}

	/**
	 * 初始化控件及数据
	 */
	public void init(){
		spinner1=(Spinner)findViewById(R.id.setting_gps_manual);
		spinner2=(Spinner)findViewById(R.id.setting_gps_automatic);
		edit1=(EditText)findViewById(R.id.setting_manual_time);
		edit2=(EditText)findViewById(R.id.setting_automatic_time);
		button1=(Button)findViewById(R.id.setting_gps_ok);
	
		
		SharedPreferenceUtil sp=new SharedPreferenceUtil(this);
		String manualgps=sp.getValue(XmlNameHelper.manualGps);
		String[] manual=new String[2];
		if(manualgps==null||manualgps.equals("NetWork")){
			this.manual="NetWork";
			manual[0]="NetWork";
			manual[1]="GPS";
		
		}else{
			this.manual="GPS";
			manual[1]="NetWork";
			manual[0]="GPS";
		}
		String[] autoGps=new String[2];
		String auto=sp.getValue(XmlNameHelper.automaticGps);
		if(auto==null||auto.equals("GPS")){
			this.automatic="GPS";
			autoGps[1]="NetWork";
			autoGps[0]="GPS";
		}else{
			this.automatic="NetWork";
			autoGps[0]="NetWork";
			autoGps[1]="GPS";
		}
		 ArrayAdapter<String> ada1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,manual);//放数据
		 ArrayAdapter<String> ada2=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,autoGps);//放数据
		 ada1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置打开方式
	       spinner1.setAdapter(ada1);
	       ada2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置打开方式
	       spinner2.setAdapter(ada2);
	       edit1.setText(sp.getValue(XmlNameHelper.manualTime));
	       edit2.setText(sp.getValue(XmlNameHelper.automaticTime));
		 
	}
	
	/**
	 * 添加监听器
	 */
      public void Listent(){
    	  
    	  button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			   SettingDao dao=new SettingDao(SettingGpsActivity.this);
			boolean b=   dao.saveSetingGps(manual, edit1.getEditableText().toString(),
					            automatic, edit2.getEditableText().toString());
			   
			   if(b){
				   Toast.makeText(SettingGpsActivity.this, "操作成功", Toast.LENGTH_LONG).show();
			   }else{
				   Toast.makeText(SettingGpsActivity.this, "操作失败", Toast.LENGTH_LONG).show();
			   }
				
			}
		});
    	    	  
    	  spinner2.setOnItemSelectedListener(new  OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				automatic=arg0.getItemAtPosition(arg2).toString();
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
    	  spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
			manual=arg0.getItemAtPosition(arg2).toString();
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
    		  
		});
      }
	
}
